uniform vec4 m_Color;
uniform int m_GridSize;

#ifdef HAS_GRIDMAP
    uniform sampler2D m_GridMap;
#endif

varying float dist;


#ifdef HAS_COLORMAP
    uniform sampler2D m_ColorMap;
#endif

varying vec2 texCoord1;


#ifdef HAS_LIGHTMAP
    uniform sampler2D m_LightMap;
    #ifdef SEPARATE_TEXCOORD
        varying vec2 texCoord2;
    #endif
#endif

#ifdef HAS_VERTEXCOLOR
    varying vec4 vertColor;
#endif

void main(){
    vec4 color = vec4(1.0);

    #ifdef HAS_COLORMAP
        color *= texture2D(m_ColorMap, texCoord1 * float(m_GridSize) * 0.5);
    #endif

    #ifdef HAS_VERTEXCOLOR
        color *= vertColor;
    #endif

    #ifdef HAS_COLOR
        color *= m_Color;
    #endif

    #ifdef HAS_LIGHTMAP
        #ifdef SEPARATE_TEXCOORD
            color.rgb *= texture2D(m_LightMap, texCoord2).rgb;
        #else
            color.rgb *= texture2D(m_LightMap, texCoord1).rgb;
        #endif
    #endif
                   
    
    vec4 cellColor =vec4(1.0);
    #ifdef HAS_GRIDMAP
        cellColor = texture2D(m_GridMap,texCoord1);
    #endif  
    float gridValue = clamp(step(dist*0.003,fract(texCoord1.x * float(m_GridSize))) *  step(dist*0.003,fract(texCoord1.y * float(m_GridSize))),0.4,1.0);


    gl_FragColor = color * cellColor * vec4(gridValue);
}